package com.bugbuff.dubbo.server.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bugbuff.common.mapper.UserInfoMapper;
import com.bugbuff.common.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public UserInfo queryUserInfoByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.userInfoMapper.selectOne(queryWrapper);
    }

    public List<UserInfo> queryUserInfoList(QueryWrapper<UserInfo> queryWrapper) {
        return this.userInfoMapper.selectList(queryWrapper);
    }

    /**
     * 根据用户id列表查询用户信息
     *
     * @param userIdList
     * @return
     */
    public List<UserInfo> queryUserInfoByUserIdList(Collection<?> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        return this.queryUserInfoList(queryWrapper);
    }

    /**
     * 更改用户信息
     * @param userInfo
     * @return
     */
    public boolean updateUserInfoByUserId(UserInfo userInfo){
        QueryWrapper<UserInfo> query=new QueryWrapper<>();
        query.eq("user_id",userInfo.getUserId());
        return userInfoMapper.update(userInfo,query)>0;
    }

    /**
     * 条件分页查询用户
     * @param queryWrapper
     * @param page
     * @param pagesize
     * @return
     */
    public List<UserInfo> queryUserInfoListByPage(QueryWrapper<UserInfo> queryWrapper, Integer page, Integer pagesize) {
        IPage<UserInfo> userInfoIPage = userInfoMapper.selectPage(new Page<>(page, pagesize), queryWrapper);
        return userInfoIPage.getRecords();
    }
}
